package service.impl;

import dao.IGoodsDao;
import dao.IOrderDao;
import dao.ITradeDao;
import dao.IUserDao;
import model.Goods;
import model.Order;
import model.Trade;
import model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import service.IOrderService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @aythor:侯仪澳
 * @data:2022/7/21
 */
@Service
public class OrderService implements IOrderService {
    @Autowired
    private IOrderDao orderDao;
    @Autowired
    private IUserDao userDao;
    @Autowired
    private IGoodsDao goodsDao;
    @Autowired
    private ITradeDao tradeDao;

    //查询所有订单分页
    @Override
    public Map<String,Object> orderLimitPage(Order order,Integer pageNo) {
        if (pageNo == null || pageNo < 1){
            pageNo = 1;
        }
        int rowCount = 10; //每一页行数
        int limitFirst = (pageNo-1)*rowCount;//计算数据库分页行号
        Map<String, Object> map = new HashMap<>();
        map.put("order", order);
        map.put("rowCount", rowCount);
        map.put("limitFirst", limitFirst);
        List<Order> list = orderDao.orderLimitPage(map);
        //计算总页数
        List<Order> list1 = orderDao.queryAllOrder(order);//获得所有的数据总数
        int totalPage = (int)(list1.size() / rowCount);
        if (list1.size() % rowCount != 0 ) {
            totalPage += 1;
        }
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("list", list);
        resMap.put("pageNo", pageNo);
        resMap.put("totalPage", totalPage);
        return resMap;
//        return orderDao.queryAllOrder(order);
    }

    //查询所有订单
    @Override
    public List<Order> queryAllOrder(Order order) {
        return orderDao.queryAllOrder(order);
    }

    //一键发货状态值
    @Override
    public String updateSendOrderBatch(List<Order> list) {
        List<Order> list1 = new ArrayList<>();
        for (int i = 0;i < list.size();i++){
            if (list.get(i).getOrderStatus() == 2){
                list1.add(list.get(i));
            }
        }
        if (list1 == null || list1.isEmpty()){
            return "0";
        }else{
            int count = orderDao.updateOrderBatch(list1);
            if (count == 1){
                return "1";
            }else {
                return "0";
            }
        }
    }

    //一键退款修改货物状态值  给用户增加钱  给货物增加库存数量
    @Override
    @Transactional//加事务
    public String updateRefundOrderBatch(List<Order> list) {
        List<Order> list1 = new ArrayList<>();
        //将前端传入的集合取出放入实体类中（再加入状态值），再放入新的集合中
        for (int i = 0;i < list.size();i++){
//            list1.add(new Order(list.get(i),5));
            Order order = orderDao.queryOneOrder(list.get(i));
            //1、修改货物状态值
            int count = orderDao.updateOneOrderBatch(list.get(i));
            if (count < 1) {
                return "0";
            }
            //2、给用户增加钱
            User user = new User();
            user.setId(order.getuId());
            user.setuMoney(order.getOrderMoney());
            int count1 = userDao.addUserMoney(user);
            if (count1 < 1) {
                return "0";
            }
            //3、给货物增加库存数量
            Goods goods = new Goods();
            goods.setId(order.getgId());
            goods.setgCount(order.getBuyCount());
            int count2 = goodsDao.addGoodsCount(goods);
            if (count2 < 1) {
                return "0";
            }
            //生成退款交易流水，交易类型3-退款
            Trade trade = new Trade();
            trade.setuId(order.getuId());
            trade.setTradeType(3);
            trade.setOrderNo(order.getOrderNo());
            trade.setTradeMoney(order.getOrderMoney());
            int count3 = tradeDao.addOneTrade(trade);
            if (count3 < 1) {
                return "0";
            }
        }
        return "1";
    }

    @Override
    public String updateOrderStatus(Order order) {
        int count = orderDao.updateOrderStatus(order);
        if (count == 1){
            return "1";
        }else {
            return "0";
        }
    }

    //取消退款，显示原来的状态值
    @Override
    public String cancelOrderStatus(Order order) {
//        order.setOrderStatus(5);//查询5-退款中的订单,获得原来的状态值
        Order order2 = orderDao.queryOneOrder(order);

        Order order1 = new Order();
        order1.setOrderNo(order.getOrderNo());
        order1.setOrderStatus(order2.getPreStatus());
        int count = orderDao.updateOrderStatus(order1);
        if (count == 1){
            return "1";
        }else {
            return "0";
        }
    }

    @Override
    public int jobForOrder() {
        return orderDao.jobForOrder();
    }

}
